package com.lifeng.patterns.singleton;

/**
 * Created by lifeng on 2019/9/11.
 */
public class SingleTwo {
    /**私有化构造方法*/
    private SingleTwo(){}
    /**私有静态引用指向自己实例 */
    private static SingleTwo instance = null;
    /**以自己实例为返回值的公有静态方法*/
    public static SingleTwo getInstance(){
        //多个线程判断instance都为null时，在执行new操作时多线程会出现重复情况
        /*if(instance == null){
            instance = new SingleTwo();
        }*/

        //==============================
        //双重检查,防止多线程时创建多个实例
        if(instance == null){
            synchronized (SingleTwo.class){
                if(instance == null){
                    instance = new SingleTwo();
                }
            }
        }
        return instance;
    }
}
